package cn.itcast.service.impl;

import cn.itcast.mapper.evaluateMapper;
import cn.itcast.pojo.evaluate;
import cn.itcast.pojo.evaluateExample;
import cn.itcast.pojo.order;
import cn.itcast.service.evaluateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 辉
 * @title: evaluateServiceImpl
 * @projectName wx_shop
 * @description: TODO
 * @date 2019/5/3010:47
 */
@Service
@Slf4j
public class evaluateServiceImpl implements evaluateService {

    @Resource
    evaluateMapper evaluateMapper;

    @Resource
    cn.itcast.mapper.orderMapper orderMapper;

    @Override
    public Integer addEvaluate(evaluate evaluate) {
        //判断是否添加成功
        boolean isadd = false;
        if (StringUtils.isNotBlank(ObjectUtils.toString(evaluate.getEvaluateContent()))
                && StringUtils.isNotBlank(ObjectUtils.toString(evaluate.getEvaluateGrade()))
                && StringUtils.isNotBlank(ObjectUtils.toString(evaluate.getOpenId()))
                && StringUtils.isNotBlank(ObjectUtils.toString(evaluate.getOrderId()))){
            int i =evaluateMapper.insertSelective(evaluate);
            order order = new order();
            order.setOrderState("已评价");
            order.setId(evaluate.getOrderId());
            orderMapper.updateByPrimaryKeySelective(order);
            if(i==1){
                isadd = true;
                log.debug("插入数据成功");
            }else{
                log.debug("插入数据有误，不为1条");
            }
        }else {
            log.debug("数据缺少，某一字段为空");
        }
        return evaluate.getEvaluateId();
    }

    /**
     *功能：查找订单评价
     * @author 欧阳辉
     * @date 2019/6/18
     * @param orderId
     * @return java.util.List<cn.itcast.pojo.evaluate>
     */
    @Override
    public evaluate findOrderEvaluate(Integer orderId,String openId) {
        if (StringUtils.isNotBlank(ObjectUtils.toString(orderId))){
            evaluateExample evaluateExample = new evaluateExample();
            cn.itcast.pojo.evaluateExample.Criteria criteria = evaluateExample.createCriteria();
            criteria.andOrderIdEqualTo(orderId);
            criteria.andOpenIdEqualTo(openId);
           return evaluateMapper.selectByExample(evaluateExample).get(0);

        }else {
            log.debug("数据缺少，某一字段为空");
        }
        return null;
    }

    @Override
    public boolean uploadEvaluate(String id, String path) {
        evaluate evaluate = evaluateMapper.selectByPrimaryKey(Integer.parseInt(id));
        evaluate.setEvaluateId(Integer.parseInt(id));
        evaluate.setEvaluatePicture(evaluate.getEvaluatePicture()+","+path);
        int i = evaluateMapper.updateByPrimaryKeySelective(evaluate);
        if(i==1){
            return true;
        }else{
            return false;
        }
    }
}
